package com.cnhis.cloudhealth.clinical.hosdoctor.clinicalpathway.bo;


import com.cnhis.cloudhealth.clinical.hosdoctor.clinicalpathway.dao.PathwayManageDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.MySqlUtil;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by xiezhenyuan on 2016/3/24.
 */
@Service
public class PathwayManageBo {
    @Resource
    private PathwayManageDao pathwayManageDao;
    @Autowired
    GetNewId getNewId;

    public ModelVo selectNianlingdanwei(){return MySqlUtil.packModelVo("PathwayManage.age_unit",null,pathwayManageDao);}
    public ModelVo selectInAndOutDep(){return MySqlUtil.packModelVo("PathwayManage.in_and_out_dep",null,pathwayManageDao);}
    public ModelVo selectShiyongkeshi(Map map){return MySqlUtil.packModelVo("PathwayManage.11944",map,pathwayManageDao);}
    public ModelVo selectLujingbianzhong(Map map){return MySqlUtil.packModelVo("PathwayManage.11945",map,pathwayManageDao);}
    public ModelVo selectMuluxinxi(Map map){return MySqlUtil.packModelVo("PathwayManage.11946",map,pathwayManageDao);}
    public ModelVo selectShiyongbingqing(){return MySqlUtil.packModelVo("PathwayManage.11947",null,pathwayManageDao);}
    public ModelVo selectBinglifenxing(){return MySqlUtil.packModelVo("PathwayManage.11948",null,pathwayManageDao);}
    public ModelVo selectXinxiliebiao(){return MySqlUtil.packModelVo("PathwayManage.11949",null,pathwayManageDao);}
    public ModelVo selectShiyongxingbie(){return MySqlUtil.packModelVo("PathwayManage.11953",null,pathwayManageDao);}
    public ModelVo selectXiangmuleixing(){return MySqlUtil.packModelVo("PathwayManage.12063",null,pathwayManageDao);}
    public ModelVo selectShengchengfangshi(){return MySqlUtil.packModelVo("PathwayManage.12064",null,pathwayManageDao);}
    public ModelVo selectXiangmuneirong(Map map){return MySqlUtil.packModelVo("PathwayManage.14403",map,pathwayManageDao);}
    public ModelVo selectNeirongyaoqiu(){return MySqlUtil.packModelVo("PathwayManage.14404",null,pathwayManageDao);}
    public ModelVo selectLujingzhixingzhe(){return MySqlUtil.packModelVo("PathwayManage.14405",null,pathwayManageDao);}
    public ModelVo selectYizhuneirong(Map map){return MySqlUtil.packModelVo("PathwayManage.14406",map,pathwayManageDao);}
    public ModelVo selectYizhuqixiao(){return MySqlUtil.packModelVo("PathwayManage.14407",null,pathwayManageDao);}
    public ModelVo selectZhixingfangshi(){return MySqlUtil.packModelVo("PathwayManage.14410",null,pathwayManageDao);}
    public ModelVo selectZhixingxingzhi(){return MySqlUtil.packModelVo("PathwayManage.14411",null,pathwayManageDao);}
    public ModelVo isKeshishiyong(Map map){return MySqlUtil.packModelVo("PathwayManage.16498",map,pathwayManageDao);}

    //Master_BIB_Edit (临床路径保存和编辑)
    public ModelVo masterBibEdit(Map map) throws Exception {
        ModelVo mo = null;
        String lxml = MapUtils.getString(map,"lxml");
        Integer fMode = MapUtils.getInteger(map,"fMode",-1);
        Long bce01 = MapUtils.getLong(map,"bce01");
        String bce03 = MapUtils.getString(map,"bce03");
        Long bib01 = MapUtils.getLong(map,"bib01",0l);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String ldate = sdf.format(new Date());
        map.put("adate",ldate);
        if(StringUtils.isBlank(lxml)){
            return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，路径相关信息为空！");
        }
        JSONObject job = JSONObject.fromObject(lxml);
        if(job==null||job.isEmpty()){
            return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，路径相关信息格式不正确，请检查！");
        }
        List<Map<String,Object>> tmpBIB = JsonUtil.parserToList(job.getJSONArray("bib1"));
        if(tmpBIB==null||tmpBIB.isEmpty()){
            return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，路径相关信息为空，请检查！");
        }else{
            Integer cnt1 = (Integer)pathwayManageDao.queryForExtObject("PathwayManage.mastr_bib1_edit_001", map);
            String astr = "000000";
            if(fMode==0) {
                for (Map<String, Object> m1 : tmpBIB) {
                    ++cnt1;
                    bib01 = getNewId.nextId();
                    Long bif01 = getNewId.nextId();
                    Integer bib13 = MapUtils.getInteger(map,"bib13",0);
                    Integer bib23 = MapUtils.getInteger(map,"bib23",0);
                    Integer bif18 = MapUtils.getInteger(map,"bif18",0);
                    String bib03 = StringUtils.substring(astr,0,StringUtils.length(astr)-StringUtils.length(cnt1.toString()))+cnt1;
                    m1.put("bib03", bib03);
                    m1.put("bib13", bib13);
                    m1.put("bib23", bib23);
                    m1.put("bif01", bif01);
                    m1.put("bif12", bce03);
                    m1.put("bif13", ldate);
                    m1.put("bib01", bib01);
                    m1.put("bif03", bif01);
                    m1.put("bif18", bif18);
                }
                map.put("tmpBIB",tmpBIB);
                pathwayManageDao.insert("PathwayManage.mastr_bib1_edit_002",map);
                pathwayManageDao.insert("PathwayManage.mastr_bib1_edit_003",map);
            }else if(fMode==1){
                for (Map<String, Object> m2 : tmpBIB) {
                    ++cnt1;
                    Integer bib13 = MapUtils.getInteger(map,"bib13",0);
                    Integer bib23 = MapUtils.getInteger(map,"bib23",0);
                    Integer bif18 = MapUtils.getInteger(map,"bif18",0);
                    String bib03 = StringUtils.substring(astr,0,StringUtils.length(astr)-StringUtils.length(cnt1.toString()))+cnt1;
                    m2.put("bif03", bib03);
                    m2.put("bib13", bib13);
                    m2.put("bib23", bib23);
                    m2.put("bif18", bif18);
                    map.put("bib1",m2);
                    map.put("bif1",m2);
                    pathwayManageDao.update("PathwayManage.mastr_bib1_edit_004",map);
                    pathwayManageDao.update("PathwayManage.mastr_bib1_edit_005",map);

                }
            }
            map.put("bib01",bib01);
            pathwayManageDao.delete("PathwayManage.mastr_bib1_edit_006",map);
            pathwayManageDao.delete("PathwayManage.mastr_bib1_edit_007",map);
        }
        List<Map<String,Object>> tmpBID = JsonUtil.parserToList(job.getJSONArray("bid1"));
        if(tmpBID==null||tmpBID.isEmpty()){
            return new ModelVo(StaticKeys.OH_FALSE, "提交信息错误，请指定使用科室！");
        }else{
            Long bid01 = getNewId.nextId();
            map.put("tmpBID",tmpBID);
            for(Map<String,Object> m3:tmpBID){
                m3.put("bid01",bid01);
                m3.put("bib01",bib01);

            }
            pathwayManageDao.insert("PathwayManage.mastr_bib1_edit_008",map);
        }
        List<Map<String,Object>> tmpBIC = JsonUtil.parserToList(job.getJSONArray("bic1"));
        if(tmpBIC==null||tmpBIC.isEmpty()){
            return new ModelVo(StaticKeys.OH_FALSE, "请指定路径使用病种！");
        }else{
            Long bic01 = getNewId.nextId();
            map.put("tmpBIC",tmpBIC);
            for(Map<String,Object> m4:tmpBIC){
                m4.put("bic01",bic01);
                m4.put("bib01",bib01);
            }
            pathwayManageDao.insert("PathwayManage.mastr_bib1_edit_009",map);
        }

        if(fMode==1 && (fMode > 0)) {
            mo = new ModelVo(StaticKeys.OH_SUCCESS, "路径修改成功！");
        }else{
            mo = new ModelVo(StaticKeys.OH_SUCCESS, "路径保存成功！");
        }
        return mo;
    }
}
